package 笔试;

public class 计算单词个数 {
    public static int cal_words_num (String sentences) {
        // write code here

        if(sentences == null || sentences.length() == 0) {
            return 0;
        }
        int count = 0;
        String[] arr = sentences.split(" ");

        count = arr.length;
        for (int i = 0; i < arr.length; i++) {
            String tmp = arr[i].trim();
            if(tmp.length() == 0) {
                count--;
            }
        }
//        boolean[] dp = new boolean[sentences.length()];
//        for(int i = 0; i < sentences.length(); i++) {
//            if(i == 0 &&  sentences.charAt(i) == ' ') {
//                continue;
//            } else if(i > 0 && sentences.charAt(i) == ' ' && dp[i-1] == false) {
//                count++;
//                dp[i] = true;
//            }
//        }
//         String[] arr = sentences.split(" ");

//         int count = arr.length;

//         for(int i = 0; i < arr.length; i++) {
//             if(arr[i] == "") {
//                 count--;
//             }
//         }

//         HashSet<String> set = new HashSet<>();

//         for(int i = 0; i < arr.length; i++) {
//             set.add(arr[i]);
//         }


//         return set.size();
        return count;

    }

    public static void main(String[] args) {
        String s = "a a    a   a";
        int count = cal_words_num(s);
        System.out.println(count);
    }
}
